package jmine.tec.environment.db.server;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import jmine.tec.persist.schema.api.SqlDialect;
import jmine.tec.persist.schema.impl.dialects.MySQLDialect;
import bancosys.tec.utils.db.converter.DbConverter;
import bancosys.tec.utils.db.converter.OracleToMysqlDbConverter;
import bancosys.tec.utils.db.executor.DbExecutor;
import bancosys.tec.utils.db.executor.MysqlDbExecutor;

/**
 * @author Rafael Volpato (May 11, 2007)
 * @author galmeida (Oct 3, 2008)
 */
public class MySQLTestDBServer extends AbstractTestDBServer {

    private static final int TIMEOUT = 300;

    /**
     * {@inheritDoc}
     * 
     * @throws SQLException
     */
    public void clearDBData(String... skipTables) throws SQLException {
        Connection conn = null;
        try {
            conn = this.getConnection();
            List<String> tables = this.getTableNames(conn);

            Statement st = conn.createStatement();
            st.setQueryTimeout(TIMEOUT);

            /* Disable constraints check */
            st.executeUpdate("SET FOREIGN_KEY_CHECKS = 0;");

            /* Delete table data */
            for (String table : tables) {
                st.executeUpdate("DELETE FROM " + table);
            }

            /* Enable constraints check */
            st.executeUpdate("SET FOREIGN_KEY_CHECKS = 1;");
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new TestDBServerException(e);
            }
        }
    }

    /**
     * Não funciona no mysql
     * 
     * @throws SQLException nunca
     */
    public void dropTables() throws SQLException {
        Connection conn = null;
        try {
            conn = this.getConnection();
            List<String> tables = this.getTableNames(conn);

            Statement st = conn.createStatement();

            /* Disable constraints check */
            st.executeUpdate("SET FOREIGN_KEY_CHECKS = 0;");

            /* Delete table data */
            for (String table : tables) {
                st.executeUpdate("DROP TABLE " + table);
            }

            /* Enable constraints check */
            st.executeUpdate("SET FOREIGN_KEY_CHECKS = 1;");
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new TestDBServerException(e);
            }
        }
    }

    /**
     * @return O DbExecutor que deve ser utilizado oara este DbServer
     */
    public DbExecutor getDbExecutor() {
        return new MysqlDbExecutor(this.getDbName());
    }

    /**
     * @param inputFile O arquivo com o schema de criação do banco
     * @param outputFile O arquivo convertido
     * @param dbName O nome do banco
     * @return o DbConverter
     */
    public DbConverter getDbConverter(URL inputFile, File outputFile, String dbName) {
        return new OracleToMysqlDbConverter(inputFile, outputFile, dbName);
    }

    /**
     * {@inheritDoc}
     */
    public SqlDialect getDialect() {
        return new MySQLDialect();
    }
}
